package pub.cleangao.common.view;


import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * 分页参数和数据结果返回
 *
 * @param <E> 结果集中的数据类型
 */
@Schema(title = "分页数据返回")
@Data
public class Page<E> implements Serializable {


    @Schema(title = "1:执行成功,消息有价值 0:执行成功 -1:不符合执行条件或错误结束 -3:无权限 -4:运行时异常")
    private int code = 0;

    @Schema(title = "提示消息")
    private String message = "SUCCESS";

    /**
     * 结果集
     */
    @Schema(title = "结果集")
    private List<E> data;
    /**
     * 总记录数
     */
    @Schema(title = "总记录数")
    private long count = 0L;

    /**
     * 当前第几页
     */
    @Schema(title = "当前第几页")
    private int pageNo = 1;

    /**
     * 每页记录数
     */
    @Schema(title = "每页记录数")
    private int pageSize = 35;
    /**
     * 总页数
     */
    @Schema(title = "总页数")
    private int maxPageNo = 0;

    @Schema(title = "从数据库第几条开始(起始:0)")
    private long offset;


    /**
     * limit [sinceIndex],[pageSize]
     */
    public long sinceIndex() {
        return (pageNo - 1) * (long) pageSize;
    }


    public static <E> Page<E> fromData(Page<E> param, Collection<E> data) {
        Page<E> result = new Page<>();
        result.setData(new ArrayList<>(data));
        result.setPageNo(param.getPageNo());
        result.setPageSize(param.getPageSize());
        result.setCode(0);
        return result;
    }

}
